#ifndef _CEXPERT_DBCHECKER_H_
#define _CEXPERT_DBCHECKER_H_

#include <pthread.h>

#include <stdexcept>
#include <string>
#include <time.h>

#include "ShMem.h"

namespace CExpert{

	class DBCheckerException : public std::runtime_error{
	public:
	DBCheckerException(const std::string& s) : std::runtime_error(s) {}
	};

	class DBChecker{
	public:
		struct conn_params{
			std::string driver;
			std::string username;
			std::string passwd;
			std::string host;
			std::string port;
			std::string db;
            std::string extra;
		};
		struct status{
			bool is_ok;
			time_t check_time;
		};

		static std::string connstr2filename(const char* connection_str);

		DBChecker(const char* connection_str, bool is_writable);
		~DBChecker();

		void start(void *(*start_routine)(void*));
        void stop();
        void join();

		const conn_params& get_cp() const;
		bool is_stopped() const;
		void fail();
		void ok();
		bool is_ok() const;
		status get_status() const;

	private:
		std::string connection_str_;
		conn_params cp_;
		ShMem sh_mem_;
		pthread_t thread_;
		volatile bool is_stopped_;
        volatile int fail_count_;
	};
}

#endif
